<?php
/**
 * Class for administration and abstraction of databases 
 * @author Gabriel Ricci - gabrielricci@gmail.com
 * @version 0.3
 */ 
 class DBManager{
 	/**
 	 * The instance of this object
 	 *
 	 * @var Object
 	 */
 	static private $instance = null;
 	
	/** constants */
 	const TYPE_MYSQL				= "mysql";
 	const TYPE_POSTGRESQL	= "postgresql";
 	const TYPE_MSSQL				= "mssql";
 	
 	const TRANSACTION_COMMIT		= "commit";
 	const TRANSACTION_ROLLBACK	= "rollback";

	/**
  	 * Singleton
  	 * @name getInstance
  	 * @access public
  	 * @return DBManager
  	 */
  	public static function getInstance(){
  		if(self::$instance == NULL){
  			self::$instance = new DBManager();
  		}
  		return self::$instance;
  	}
  	
 	/**
  	 * Begin a transaction 
 	 * @name beginTransaction
 	 * @access public
 	 * @param String $connection Connection name
 	 * @param boolean $autocommit True for autocommit the transaction on the end of the excecution
 	 * @return boolean
 	 * @static 
 	 */
 	public static function beginTransaction($connection = 'default', $autocommit = false){
 		// get the connection
 		$connection = DBMConnectionManager::getInstance()->getConnection($connection);
 		
 		// begin the transaction
 		return $connection->beginTransaction($autocommit);
 	}
 	
 	/**
 	 * Rollback an opened connection 
 	 * @name rollbackTransaction
 	 * @access public
 	 * @param String $identification Connection name
 	 * @return boolean
 	 * @static 
 	 */
 	public function rollbackTransaction($connection = 'default'){
 		// get the connection
 		$connection = DBMConnectionManager::getInstance()->getConnection($connection);
 		
 		// rollback
 		return $connection->rollbackTransaction();
 	}
 	
 	/**
  	 * Commit an opened transaction
 	 * @name commitTransaction
 	 * @access public
 	 * @param String $identification Connection name
 	 * @return boolean
 	 * @static 
 	 */
 	public function commitTransaction($connection = 'default'){
 		// get the connection
 		$connection = DBMConnectionManager::getInstance()->getConnection($connection);
 		
 		// commit
 		return $connection->commitTransaction();
 	}
  	
 	/**
 	 * Execute a query
 	 * @param string $sql Query to be executed
 	 * @param string $identification Connection identification
 	 */
  	public function runQuery($sql, $identification = "default"){
  		return DBMConnectionManager::getInstance()->getConnection($identification)->runQuery($sql);
  	}
}
?>